<template>
  <div class="add-account-container">
    <h2 class="title">添加账号</h2>
    
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      label-position="right"
      style="max-width: 600px"
    >
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="formData.account"
          placeholder="请输入账号"
          clearable
        />
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="formData.password"
          placeholder="请输入密码"
          type="password"
          show-password
          clearable
        />
      </el-form-item>
      
      <el-form-item label="用户组" prop="userGroup">
        <el-select
          v-model="formData.userGroup"
          placeholder="请选择用户组"
          style="width: 100%"
        >
          <el-option
            v-for="item in groupOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { addAccount } from '@/api/account'

// 表单引用
const formRef = ref()

// 表单数据
const formData = ref({
  account: '',
  password: '',
  userGroup: ''
})

// 用户组选项
const groupOptions = [
  { value: '超级管理员', label: '超级管理员' },
  { value: '编辑人员', label: '编辑人员' },
  { value: '普通用户', label: '普通用户' }
]

// 表单验证规则
const formRules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
  ],
  userGroup: [
    { required: true, message: '请选择用户组', trigger: 'change' }
  ]
}


// 提交表单
const submitForm = () => {
  console.log(formData.value)
  addAccount(formData.value).then(res => {
    console.log(res)
    ElMessage.success('添加成功')
  })
}
// 重置表单
const resetForm = () => {
  formRef.value.resetFields()
}
</script>

<style scoped>
.add-account-container {
  padding: 20px;
}

.title {
  margin-bottom: 30px;
  color: #303133;
}
</style>